<?php
/**
 * Created by PhpStorm.
 * User: lixy
 * Date: 2016/4/25
 * Time: 13:22
 */
namespace app\models;

use app\lib\WebService;
use app\models\weixin\T_WXMALL_DORACARD;
use Yii;
use app\lib\BaseModel;
use app\lib\WeixinUtils;


class WxGoodDao extends BaseModel
{
    //添加商品到购物车  --ps：界面没有选个数的，所以添加的个数都是1
    public static function add_art($userid, $goods_code)
    {

        $cmd = static::query()->createCommand('select count(1) from t_wxmall_goods where goods_code=:goods_code ');
        $cmd->bindValue(':goods_code', $goods_code);

        if ($cmd->queryScalar() == 0) {
            return "没有这个产品";
        }
        $cmd = static::query()->createCommand('select count(1) from t_wx_goods_cart where goods_code=:goods_code and user_id=:user_id');
        $cmd->bindValue(':goods_code', $goods_code);
        $cmd->bindValue(':user_id', $userid);
        $isExist = $cmd->queryScalar();
        if ($isExist == 0) {
            $cmd = static::query()->createCommand('insert into t_wx_goods_cart (id,user_id,goods_code,count,is_selected) VALUES  (:id,:user_id,:goods_code,1,0)');
            $cmd->bindValue(':goods_code', $goods_code);
            $cmd->bindValue(':user_id', $userid);
            $cmd->bindValue(':id', static::uuid());
            $re = $cmd->execute();
        } else {
            $cmd = static::query()->createCommand('update  t_wx_goods_cart set count=count+1 where  goods_code=:goods_code and user_id=:user_id');
            $cmd->bindValue(':goods_code', $goods_code);
            $cmd->bindValue(':user_id', $userid);
            $re = $cmd->execute();
        }
        return $re > 0 ? 0 : 1;

    }

    //计算购物车总数量
    public static function calc_cart_order_TOTALNUM($userid)
    {
        $sql = "SELECT count(1) FROM t_wx_goods_cart WHERE user_id =:user_id ";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':user_id', $userid);
        $result = $cmd->queryScalar();
        return $result;
    }

    //取得购物车产品数量
    public static function calc_cart_goods_TOTALNUM($userid, $goods_code)
    {
        $sql = "SELECT count(1) FROM t_wx_goods_cart WHERE user_id =:user_id and goods_code =:goods_code ";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':user_id', $userid);
        $cmd->bindValue(':goods_code', $goods_code);
        $result = $cmd->queryScalar();
        return $result;
    }

//
//        public static function get_shopping_cartList($userid){
//                $sql="SELECT t.goods_code,g.goods_name,t.count, nvl(g.price,0) as price,g.goods_img_url as image_url FROM t_wx_goods_cart t


    //显示购物车内容
    /**
     * @param $userid
     * @return ARRAYLIST{count goods_code goods_name count price image_url}
     */
    public static function get_shopping_cartList($userid)
    {
        $sql = "SELECT t.goods_code,g.goods_name,t.count, nvl(g.price,0) as price,g.goods_img_url as image_url FROM t_wx_goods_cart t
              LEFT JOIN t_wxmall_goods g ON g.goods_code = t.goods_code   WHERE t.user_id=:user_id ";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':user_id', $userid);
        $result = $cmd->queryAll();
        return $result;
    }


    //增加或者减少购物车商品购买数量
    public static function inc_cart_num($userid, $goods_code, $addnum)
    {
        $cmd = \Yii::$app->db->createCommand('select count from t_wx_goods_cart  where user_id=:user_id and goods_code=:goods_code');
        $cmd->bindValue(':user_id', $userid);
        $cmd->bindValue(':goods_code', $goods_code);
        $re = $cmd->queryScalar();
        if ($re == false) {
            return 0;
        }
        if ($re <= 1 && $addnum < 0) {
            return 0;
        }

        $cmd = \Yii::$app->db->createCommand('update t_wx_goods_cart set count=count+:addnum where user_id=:user_id and goods_code=:goods_code');
        $cmd->bindValue(':user_id', $userid);
        $cmd->bindValue(':goods_code', $goods_code);
        $cmd->bindValue(':addnum', $addnum);
        return $cmd->execute();
    }

    //删除购物车某产品
    public static function delete_cart_goods($userid, $goods_code)
    {
        $cmd = \Yii::$app->db->createCommand('delete from t_wx_goods_cart where user_id=:user_id and goods_code=:goods_code');
        $cmd->bindValue(':user_id', $userid);
        $cmd->bindValue(':goods_code', $goods_code);
        return $cmd->execute();
    }

    //删除购物车
    public static function delete_cart($userid)
    {
        $cmd = \Yii::$app->db->createCommand('delete from t_wx_goods_cart where user_id=:user_id');
        $cmd->bindValue(':user_id', $userid);
        return $cmd->execute();
    }

    //修改购物车是否选中状态
    public static function update_cart_unselected($userid)
    {
        $cmd = \Yii::$app->db->createCommand('update t_wx_goods_cart set is_selected = 0 where user_id=:user_id');
        $cmd->bindValue(':user_id', $userid);
        return $cmd->execute();
    }

    //修改购物车是否选中状态
    public static function update_cart_selected($userid, $goods_code)
    {
        $cmd = \Yii::$app->db->createCommand('update t_wx_goods_cart set is_selected = 1 where user_id=:user_id and goods_code=:goods_code');
        $cmd->bindValue(':user_id', $userid);
        $cmd->bindValue(':goods_code', $goods_code);
        return $cmd->execute();
    }

    /**
     * 根据用户ID取得用户收货地址
     * @param $userid
     */
    public static function getReceiverAddress($userid)
    {
        $sql = "SELECT t.name,t.phone,t.province,t.city,t.region,t.address,prov.id as province_id,city.id as city_id
         from t_wx_address t left join t_wx_area prov on prov.name = t.province
         left join t_wx_area city on city.name = t.city where t.user_id =:userid ";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':userid', $userid);
        $result = $cmd->queryAll();
        return $result;
    }

    /**
     * 新增用户收货地址信息
     * @param $userid
     * @param $rece_name
     * @param $rece_phone
     * @param $rece_address
     * @param $sheng
     * @param $shi
     * @param $qu
     * @return int
     * @throws \yii\db\Exception
     */
    public static function saveReceiverAddress($userid, $rece_name, $rece_phone, $rece_address, $sheng, $shi, $qu)
    {

        $cmd = static::query()->createCommand('SELECT id FROM t_wx_address WHERE user_id=:user_id');
        $cmd->bindValue(':user_id', $userid);
        $re = $cmd->queryScalar();
        if ($re == false) {//add
            $cmd = static::query()->createCommand('insert into t_wx_address (id,user_id,name,phone,address,province,city,region,is_default)
                 VALUES (:id,:user_id,:name,:phone,:address,:province,:city,:region,0)');
            $cmd->bindValue(':id', static::uuid());
            $cmd->bindValue(':user_id', $userid);
        } else {//update
            $cmd = static::query()->createCommand('update t_wx_address set name=:name,phone=:phone,address=:address
            ,province=:province,city=:city,region=:region
                   where id=:id');
            $cmd->bindValue(':id', $re);
        }


        $cmd->bindValue(':name', $rece_name);
        $cmd->bindValue(':phone', $rece_phone);
        $cmd->bindValue(':address', $rece_address);
        $cmd->bindValue(':province', $sheng);
        $cmd->bindValue(':city', $shi);
        $cmd->bindValue(':region', $qu);
        $re = $cmd->execute();
        if ($re > 0) {
            return 0;
        } else {
            return 1;
        }

    }

    /**
     * 获取地区：省
     * @return array
     */
    public static function getAreaList_sheng()
    {
        $sql = "SELECT code,name,id FROM t_wx_area WHERE parent_id = '' or parent_id is null  ORDER BY code asc";
        $cmd = \Yii::$app->db->createCommand($sql);
        $result = $cmd->queryAll();
        return $result;

    }

    /**
     * 获取地区：市 区
     * @return array
     */
    public static function getAreaList_other($parent_id)
    {
        $sql = "SELECT code,name,id FROM t_wx_area WHERE parent_id = :parent_id ORDER BY code asc";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':parent_id', $parent_id);
        $result = $cmd->queryAll();
        return $result;
    }


    /**
     * 根据用户ID查询需结算的产品列表
     * @param $userid
     * @return ARRAYLIST
     */
    public static function getCartPayGoodsDetail($userid)
    {
        $sql = "SELECT t.goods_code,g.goods_name ,t.count,nvl(g.price,0) as price,g.goods_img_url as image_url FROM t_wx_goods_cart t
              LEFT JOIN t_wxmall_goods g ON g.goods_code = t.goods_code
                WHERE t.is_selected = 1 and t.user_id=:user_id ";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':user_id', $userid);
        $result = $cmd->queryAll();
        return $result;
    }

    /**
     * 根据用户ID查询需结算的合计金额
     * @param $userid
     * @return false|null|string
     */
    public static function getCartPayTotalprice($userid)
    {
        $sql = "  SELECT sum(nvl(t.count,0) * (nvl(g.price,0))) as totalprice FROM t_wx_goods_cart t
            LEFT JOIN t_wxmall_goods g ON g.goods_code = t.goods_code
            WHERE t.is_selected = 1 and t.user_id=:user_id ";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':user_id', $userid);
        $result = $cmd->queryScalar();
        return $result;
    }


    /**
     * 获取单个产品信息 包括图片(用于前台展示)
     * @param $goods_code
     * @return mixed
     */
    public static function get_GoodsInfoAndImgByCode($goods_code)
    {

        $sql = "select t.goods_code,t.goods_name,nvl(t.price,0) as price,t.goods_img_url as image_url
                from t_wxmall_goods t where t.goods_code=:goods_code ";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':goods_code', $goods_code);
        $result = $cmd->queryAll();
        if (count($result) > 0) {
            return $result;
        } else {
            return false;
        }
    }

    /**
     * 生成订单
     * @param $userid
     * @return int
     * @throws \yii\db\Exception
     */
    public static function createNewOrder($userid, $coin)
    {

        $sql = "call P_SAVE_ORDER(:i_userId,:coin,:o_orderId)";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':i_userId', $userid);
        $cmd->bindValue(':coin', $coin);
        $cmd->bindParam(':o_orderId', $orderId, \PDO::PARAM_STR, 50);
        $cmd->execute();;
        if ($orderId != null && $orderId != '' && $orderId != '0') {
            return $orderId;
        } else {
            return 0;
        }
    }

    /**
     * 生成订单
     * @param $userid
     * @param $goods_code
     * @return int
     * @throws \yii\db\Exception
     */
    public static function createNewOrderByGoods($userid, $goods_code, $coin, $count, $phone)
    {
        $sql = "call p_save_order_bygoods(:i_userId,:i_goods_code,:count,:coin,:phone,:o_orderId)";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':i_userId', $userid);
        $cmd->bindValue(':i_goods_code', $goods_code);
        $cmd->bindValue(':count', $count);
        $cmd->bindValue(':coin', $coin);
        $cmd->bindValue(':phone', $phone);
        $cmd->bindParam(':o_orderId', $orderId, \PDO::PARAM_STR, 50);
        $cmd->execute();

        if ($orderId != null && $orderId != '' && $orderId != '0') {
            return $orderId;
        } else {
            return 0;
        }
    }

//    /**
//     * 根据订单id查询订单详情
//     * @param $orderId
//     * @return array|false
//     */
//    public static function getOrderById($orderId)
//    {
//        $cmd = \Yii::$app->db->createCommand("
//                    SELECT t0.*, (SELECT  remark FROM t_wxmall_dict WHERE type='goods_order_status' and code=t0.status )AS status_name
//             FROM t_wx_goods_order t0
//                        WHERE t0.id=:orderId
//        ");
//        $cmd->bindValue(':orderId', $orderId);
//        $result = $cmd->queryAll();
//
//        return count($result) == 0 ? false : $result[0];
//    }

//    /**
//     * 根据订单ID查询订单产品明细
//     * @param $orderId
//     * @return ARRAYLIST
//     */
//    public static function getOrderGoodsDetail($orderId)
//    {
//        $sql = "SELECT t.goods_code,g.goods_name ,t.count,nvl(g.price,0) as price, nvl(g.price,0) as integral, g.goods_img_url as  image_url
//              FROM t_wx_goods_order_goods t
//              LEFT JOIN t_wxmall_goods g ON g.goods_code = t.goods_code
//              WHERE t.order_id=:order_id ";
//        $cmd = \Yii::$app->db->createCommand($sql);
//        $cmd->bindValue(':order_id', $orderId);
//        $result = $cmd->queryAll();
//        return $result;
//    }
//
//    /**
//     * 修改订单状态
//     * @param string $orderCode
//     * @param int $status
//     * 订单状态
//     * 1：未支付
//     * 2：支付中（积分已经扣除，等待现金扣除）
//     * 3：已支付
//     * 4：支付失败
//     * 10：出库中
//     * 11：配送中
//     * 12：配送完成
//     * 20：已取消
//     * 21：已完成
//     * 22：已失效
//     * @throws \yii\db\Exception
//     */
//    public static function setOrderStatusByCode($orderCode, $status)
//    {
//        $cmd = \Yii::$app->db->createCommand('UPDATE t_wx_goods_order SET status= :status WHERE code=:order_code');
//        $cmd->bindValue(':status', $status);
//        $cmd->bindValue(':order_code', $orderCode);
//        $cmd->execute();
//    }

    //获取订单列表
    public static function getOrderList($userId, $page = 0, $pageSize = 5)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM (
                                                  SELECT T.*,ROWNUM rn FROM (
                                                       SELECT * FROM t_wx_goods_order
                                                       WHERE user_id = :user_id
                                                       ORDER BY create_datetime DESC
                                                       ) T WHERE ROWNUM <= :rowNumEnd
                                                ) WHERE rn > :rowNumStart');
        $cmd->bindValue(':user_id', $userId);
        $cmd->bindValue(':rowNumEnd', (int)(($page + 1) * $pageSize));
        $cmd->bindValue(':rowNumStart', (int)($page * $pageSize));

        return $cmd->queryAll();
    }

    //获取订单商品
    public static function getOrderGoodsDetail($orderId)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_wx_goods_order_goods t0
                                                  INNER JOIN t_wxmall_goods t1 ON t1.goods_code = t0.goods_code
                                                  WHERE t0.order_id = :order_id');
        $cmd->bindValue(':order_id', $orderId);
        return $cmd->queryAll();
    }

    //获取订单状态
    public static function getOrderStatusText($orderStatus)
    {
        $status_text = '';
        switch ($orderStatus) {
            case 1:
                $status_text = '未支付';
                break;
            case 2:
                $status_text = '支付中';//（朵拉币已经扣除，等待现金扣除回调）
                break;
            case 3:
                $status_text = '已支付';
                break;
            case 4:
                $status_text = '支付失败';
                break;
            case 10:
                $status_text = '已配送';//出库中
                break;
            case 11:
                $status_text = '已配送';//配送中
                break;
            case 12:
                $status_text = '配送完成';
                break;
            case 20:
                $status_text = '已取消';
                break;
            case 21:
                $status_text = '已完成';
                break;
            case 22:
                $status_text = '已失效';
                break;
        }

        return $status_text;
    }

    //根据orderId查询订单信息
    public static function getOrderById($orderId)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_wx_goods_order WHERE ID = :ID');
        $cmd->bindValue(':ID', $orderId);
        $result = $cmd->queryAll();

        return count($result) > 0 ? $result[0] : false;
    }

    //根据orderId和userId查询订单信息
    public static function getOrderByIdAndUserId($orderId, $userId)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_wx_goods_order WHERE ID = :ID AND USER_ID = :USER_ID');
        $cmd->bindValue(':ID', $orderId);
        $cmd->bindValue(':USER_ID', $userId);
        $result = $cmd->queryAll();

        return count($result) > 0 ? $result[0] : false;
    }

    //修改订单状态
    public static function setOrderStatusByCode($orderId, $status)
    {
        $cmd = \Yii::$app->db->createCommand('UPDATE t_wx_goods_order SET STATUS = :STATUS WHERE ID = :ID');
        $cmd->bindValue(':ID', $orderId);
        $cmd->bindValue(':STATUS', $status);

        return $cmd->execute();
    }

    /**
     * 根据订单编号查询订单
     * @param $orderCode
     * @return array|false
     */
    public static function getOrderByCode($orderCode)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_wx_goods_order WHERE code=:orderCode');
        $cmd->bindValue(':orderCode', $orderCode);
        $result = $cmd->queryAll();
        return count($result) > 0 ? $result[0] : false;
    }

    //获取商城产品
    public static function get_GoodsList($start = 0, $pagesize = 6, $category = '0', $order = '0', $filter = '0', $openid)
    {
        $ishow = 'and t.ishow=\'Y\' ';
        /*if (T_WXMALL_DORACARD::get_User_Sale($openid) > 0) {
            $ishow = '';
        }*/
        //加排序
        if ($order == '0') {
            $order = '';
        } else {
            //1正序
            $desc = "";
            if ($order == '2') {
                $desc = "desc";
            }
            $order = " order by price  $desc ,GOODS_CODE ";
        }

        if ($category == '0') {
            $categorySQL = '';
        } else {
            $categorySQL = " and goods_cl=:category ";
        }

        if ($filter == '0') {
            $filterSQL = '';
        } else {
            $filterSQL = " and (goods_name like :filter) ";
        }

//GOODS_CLS为KJ,HD的商品为虚拟商品
        $sql = "select CASE WHEN t.GOODS_CLS='KJ' OR t.GOODS_CLS ='HD' THEN 2 ELSE 1 END TYPE,t.* from V_WXMALL_SINGLEGOODS t  where t.dr=0  $ishow $categorySQL $filterSQL $order";
//
//        var_dump($sql);
//        exit;
        $cmd = static::query()->createCommand(static::pageSql($sql, $start, $pagesize));
        if ($category != '0') {
            $cmd->bindValue(':category', $category);
        }

        if ($filter != '0') {
            $filter = '%' . $filter . '%';
            $cmd->bindValue(':filter', $filter);
        }

        return $cmd->queryAll();

    }

    //获取产品分类
    public static function get_Category($openid)
    {
        $ishow = 'and ishow=\'Y\' ';
        /*if (T_WXMALL_DORACARD::get_User_Sale($openid) > 0) {
            $ishow = '';
        }*/
        $cmd = static::query()->createCommand("select * from t_wxmall_goods_category where 1=1 $ishow");
        return $cmd->queryAll();
    }

    //获取单个产品
    public static function get_Goods($goods_code)
    {
        $cmd = static::query()->createCommand('select * from t_wxmall_goods where goods_code=:goods_code ');
        $cmd->bindValue(':goods_code', $goods_code);
        $goods = $cmd->queryAll();
        if (count($goods) > 0) {
            $goods = $goods[0];
            $cmd2 = static::query()->createCommand('select GOODS_EXHI_URL from T_WXMALL_GOODS_IMAGEINFO where imgtype=1 and GOODS_CODE=:goods_code ');
            $cmd2->bindValue(':goods_code', $goods_code);
            $imgList = $cmd2->queryAll();
            return ['goods' => $goods, 'imgList' => $imgList];
        } else {
            return false;
        }

    }

    /**
     * 订单同步到业务系统成功
     * @param $orderId
     * @throws \yii\db\Exception
     */
    public static function setOrderSyncSuccess($orderId)
    {
        $cmd = \Yii::$app->db->createCommand('UPDATE t_wx_goods_order SET is_sync_success=1 WHERE id=:order_id');
        $cmd->bindValue(':order_id', $orderId);
        return $cmd->execute();
    }

    /**
     * 通过接口向业务系统同步订单
     * @param $order
     */
    public static function _setCoreOrder($order)
    {
        if ($order['IS_SYNC_SUCCESS'] != 0) {
            return;
        }

        try {
            $orderGoodsList = self::getOrderGoodsDetail($order['ID']);
            $orderGoodsListData = array();
            foreach ($orderGoodsList as $orderGoods) {
                $orderGoodsData =
                    [
                        'goods_code' => $orderGoods['GOODS_CODE'],
                        'goods_name' => $orderGoods['GOODS_NAME'],
                        'goods_number' => $orderGoods['COUNT'],
                        'price' => $orderGoods['PRICE']
                    ];
                array_push($orderGoodsListData, $orderGoodsData);
            }
            $orderData = [
                "orderCode" => $order['CODE'],
                "ordertype" => '',
                "shrName" => $order['RECEIVER_NAME'],
                "sheng" => $order['RECEIVER_PROVINCE'],
                "shi" => $order['RECEIVER_CITY'],
                "qu" => $order['RECEIVER_REGION'],
                "address" => $order['RECEIVER_ADDRESS'],
                "mobile" => $order['RECEIVER_PHONE'],
                "deskphone" => '',
                "email" => '',
                'beizhu' => self::DevRemark(),
                "amount" => $order['PRICE'],
                "goods" => $orderGoodsListData
            ];
            $setOrderResult = WebService::goods_setWxOrder($orderData);

            if ($setOrderResult != null && $setOrderResult->status == "1") {
                static::setOrderSyncSuccess($order['ID']);
                if ($order['TYPE'] == 2) {
                    //虚拟商品记录序列号
                    $virtualCodes = '';
                    foreach ($setOrderResult->data->virtual_volume as $item) {
                        if ($virtualCodes != '') {
                            $virtualCodes = $virtualCodes . ',' . $item->xuliehao;
                        } else {
                            $virtualCodes = $item->xuliehao;
                        }
                    }
                    if ($virtualCodes != '') {
                        static::addGoodsVirtualCode($order['ID'], $virtualCodes);
                    }

                    //虚拟商品要将序列号发送到买家手机
                    require_once '../lib/SmsSdk.php';

                    $account = Yii::$app->params['SMS_ACCOUNT'];
                    $token = Yii::$app->params['SMS_TOKEN'];
                    $to = $order['RECEIVER_PHONE'];
                    $data = [$orderGoodsListData[0]['goods_name'], $virtualCodes];
                    $template_id = Yii::$app->params['SMS_TEMPLATE_ID'];;
                    $url = Yii::$app->params['SMS_URL'];

                    $smsSdk = new \SmsSdk($account, $token, $url);
                    $start_time = microtime(true);
                    $result = $smsSdk->sendSms($template_id, $to, $data);
                }

            }
        } catch (\Exception $e) {
        }
    }

    public static function getVirtualCodeListByOrderId($orderId)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_wx_goods_virtual_code WHERE ORDER_ID =  :ORDER_ID');
        $cmd->bindValue(':ORDER_ID', $orderId);
        $result = $cmd->queryAll();

        return $result;
    }

    public static function addGoodsVirtualCode($orderId, $virtualCodes)
    {
        $cmd = \Yii::$app->db->createCommand('INSERT INTO t_wx_goods_virtual_code(ORDER_ID,CODE)
                                              SELECT :ORDER_ID,REGEXP_SUBSTR(:VirtualCodes, \'[^,]+\', 1, LEVEL, \'i\') AS STR
                                              FROM DUAL CONNECT BY LEVEL <= LENGTH(:VirtualCodes) - LENGTH(REGEXP_REPLACE(:VirtualCodes, \',\', \'\')) + 1');

        $cmd->bindValue(':ORDER_ID', $orderId);
        $cmd->bindValue(':VirtualCodes', $virtualCodes);

        $re = $cmd->execute();
        if ($re > 0) {
            return 0;
        }

        return 1;
    }

    /**
     * 单品订单配送状态
     * @param $order_code
     * @param $order_delivery_code
     * @param $logistics_company
     * @param $logistics_code
     * @param $goods_list_str
     * @return int
     * @throws \yii\db\Exception
     */
    public static function setGoodsOrderDelivery($order_code, $order_delivery_code, $logistics_company, $logistics_code, $goods_list_str)
    {
//        var_dump('$order_code:'.$order_code.'; $order_delivery_code:'.$order_delivery_code.';$logistics_company:'.$logistics_company.';$logistics_code:'.$logistics_code.';$goods_list_str:'.$goods_list_str);
//        exit;

        $sql = "call P_WXMALL_SET_ORDERDELIVERY(:ORDER_CODE,:ORDER_DELIVERY_CODE,:LOGISTICS_COMPANY,:LOGISTICS_CODE,:GOODS_LIST,:OUT_ORDERDELIVERYID)";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':ORDER_CODE', $order_code);
        $cmd->bindValue(':ORDER_DELIVERY_CODE', $order_delivery_code);
        $cmd->bindValue(':LOGISTICS_COMPANY', $logistics_company);
        $cmd->bindValue(':LOGISTICS_CODE', $logistics_code);
        $cmd->bindValue(':GOODS_LIST', $goods_list_str);
        $cmd->bindParam(':OUT_ORDERDELIVERYID', $out_orderdelivery_id, \PDO::PARAM_STR, 50);
        $cmd->execute();

        if ($out_orderdelivery_id != null && $out_orderdelivery_id != '' && $out_orderdelivery_id != '0') {
            return $out_orderdelivery_id;
        }
        return 0;

    }

    /**
     * 根据order_id获取订单配送单表信息
     * @param $order_id
     * @return array
     */
    public static function getOrderDeliveryList($order_id)
    {
        $sql = 'SELECT * FROM t_wx_goods_orderdelivery WHERE GOODS_ORDER_ID = :GOODS_ORDER_ID';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':GOODS_ORDER_ID', $order_id);
        $result = $cmd->queryAll();
        return $result;
    }

    /**
     * 根据delivery_id获取订单配送单商品
     * @param $delivery_id
     * @return array
     */
    public static function getOrderdeliveryGoodsList($delivery_id)
    {
        $sql = 'SELECT t1.* FROM t_wx_goods_orderdelivery_goods t0
                  INNER JOIN t_wxmall_goods t1 ON t1.goods_code = t0.goods_code
                WHERE t0.GOODS_ORDERDELIVERY_ID = :GOODS_ORDERDELIVERY_ID';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':GOODS_ORDERDELIVERY_ID', $delivery_id);
        $result = $cmd->queryAll();
        return $result;
    }


    static function DevRemark()
    {
        if (YII_ENV == 'dev') {
            return "开发测试订单，勿发货！！！";
        } else {
            return "";
        }
    }
}